/**
 * 弹出层
 * */
layui.define(['layer', 'form', 'globalConfig'], (exports) => {
	let layer = layui.layer;
	let form = layui.form;
	let globalConfig = layui.globalConfig;
	let loadingId = null;
	let sdDialog = {
		close(index){
		    layer.close(index);
		},
		confirm(param){
		    layer.confirm(param.title, {
		        title: "温馨提示",
		        skin: "layui-layer-admin"
		    }, function(index) {
		        param.submit ? param.submit() : ""
		        layer.close(index);
		    })
		},
		confirm2(param){
		    layer.confirm(param.title, {
		        title: "操作提示",
		        btn: param.btn //按钮
		    }, function(index){
		        param.btn1 ? param.btn1() : "";
		        layer.close(index);
		    }, function(index){
		        param.btn2 ? param.btn2() : "";
		        layer.close(index);
		    }, function(index){
		        param.btn3 ? param.btn3() : "";
		        layer.close(index);
		    });
		},
		page(param) {
		    sdDialog.popupCenter({
		        title: param.title,
		        path: param.url,
		        skin: param.skin,
		        height: param.height ? param.height : '80%',
		        width: param.width ? param.width : '600px',
		        btn: param.btn ? param.btn : false,
		        init() {
		            param.init ? param.init() : '';
		        },
		        opt(index) {
		            param.opt ? param.opt(index) : '';
		        },
		        end(){
		            param.end ? param.end() : '';
		        }
		    });
		},
		form(param) {
		    sdDialog.popupCenter({
		        title: param.title,
		        height: param.height ? param.height : '80%',
		        width: param.width ? param.width : '720px',
		        path: param.path || '/components/common/form.html',
		        skin: param.skin,
		        init() {
		            param.init ? param.init() : '';
		        },
		        opt(index) {
		            param.opt ? param.opt(index) : '';
		        }
		    });
		},
		tips: {
		    ok(message) {
		        layx.notice({
		            title: '成功提示',
		            type: 'success',
		            message: message
		        });
		    },
		    error(message) {
		        layx.notice({
		            title: '错误提示',
		            type: 'error',
		            message: message
		        });
		    },
		    info(message) {
		        layx.notice({
		            title: '温馨提示',
		            message: message
		        });
		    },
		    warning(message) {
		        layx.notice({
		            title: '警告提示',
		            type: 'warning',
		            message: message
		        });
		    }
		},
		// 进度条
		loading: {
		    show() {
		        loadingId = layer.load(2);
		    },
		    hide() {
		        layer.close(loadingId);
		    }
		},
		open: function(view) {
		    if (!view.area) {
		        view.area = (view.type == 2) ? ["360px", "300px"] : "360px"
		    }
		    if (!view.offset) {
		        view.offset = "35px"
		    }
		    if (view.fixed == undefined) {
		        view.fixed = false
		    }
		    view.resize = view.resize != undefined ? view.resize : false;
		    view.shade = view.shade != undefined ? view.shade : 0.5;
			
		    let end = view.end;
		    view.end = function() {
		        layer.closeAll("tips");
		        end && end()
		    };
		    if (view.url) {
		        (view.type == undefined) && (view.type = 1);
		        let success = view.success;
		        view.success = function(s, t) {
		            let url = view.url;
		            if (globalConfig.version != undefined) {
		                if (url.indexOf("?") == -1) {
		                    url += "?v="
		                } else {
		                    url += "&v="
		                }
		                if (globalConfig.version == true) {
		                    url += new Date().getTime()
		                } else {
		                    url += globalConfig.version
		                }
		            }
		            sdDialog.showLoading(s, 2);
		            $(s).children(".layui-layer-content").load(url, function() {
		                success ? success(s, t) : "";
		                sdDialog.removeLoading(s, false)
		            })
		        }
		    }
		    return layer.open(view)
		},
		showLoading: function(s, r, q) {
		    let html = ['<div class="ball-loader"><span></span><span></span><span></span><span></span></div>', '<div class="rubik-loader"></div>'];
		    if (!s) {
		        s = "body"
		    }
		    if (r == undefined) {
		        r = 1
		    }
		    $(s).addClass("page-no-scroll");
		    let t = $(s).children(".page-loading");
		    if (t.length <= 0) {
		        $(s).append('<div class="page-loading">' + html[r - 1] + "</div>");
		        t = $(s).children(".page-loading")
		    }
		    q && t.css("background-color", "rgba(255,255,255," + q + ")");
		    t.show()
		},
		removeLoading: function(q, s, p) {
		    if (!q) {
		        q = "body"
		    }
		    if (s == undefined) {
		        s = true
		    }
		    let r = $(q).children(".page-loading");
		    if (p) {
		        r.remove()
		    } else {
		        s ? r.fadeOut() : r.hide()
		    }
		    $(q).removeClass("page-no-scroll")
		},
		popupRight: function(popup) {
		    if (popup.title == undefined) {
		        popup.title = false;
		        popup.closeBtn = false
		    }
		    if (popup.anim == undefined) {
		        popup.anim = 2
		    }
		    if (popup.fixed == undefined) {
		        popup.fixed = true
		    }
		    popup.isOutAnim = false;
		    popup.offset = "r";
		    popup.shadeClose = true;
		    popup.area = "336px";
		    popup.skin = "layui-layer-adminRight";
		    popup.move = false;
		    return sdDialog.open(popup)
		},
		routerNotFound: function() {
		    layer.open({
		        title: false,
		        skin: "layui-layer-admin",
		        btn: [],
		        offset: "30px",
		        anim: 6,
		        shadeClose: true,
		        time: 3000,
		        area: '360px',
		        offset: 'rt',
		        shade: 0,
		        closeBtn: 0,
		        anim: 2,
		        resize: false,
		        move: false,
		        btn: [],
		        icon: 2,
		        content: "你无权访问该路由或此" + location.hash + "路由不存在"
		    })
		},
		closeThisDialog: function() {
		    parent.layer.close(parent.layer.getFrameIndex(window.name))
		},
		closeDialog: function(dialog) {
		    let id = $(dialog).parents(".layui-layer").attr("id").substring(11);
		    layer.close(id)
		},
		rightView: function (name) {
		    let url = $(this).attr("data-url");
		    sdDialog.popupRight({
		        id: "layer-notice",
		        url: url ? url : "components/common/" + name + ".html"
		    })
		},
		popupCenter: function (param) {
		    let $height = (typeof param.height) == "undefined" ? '80%' : (param.height == 0 ? '80%' : param.height);
		    let $width = (typeof param.width) == "undefined" ? '600px' : param.width;
		    let $area = [$width, $height];
		    let id = "soda" + parseInt((Math.random() * 1000000000)+"");
		    let popupCenterIndex = layer.open({
		        type: 1,
		        id: id,
		        title: param.title ? param.title : false,
		        shade: .5,
		        offset: '50px',
		        area: param.area ? param.area : $area,
		        resize: false,
		        scrollbar: false,
		        skin: param.skin ? param.skin : "soda-class",
		        success: function () {
		            let $id = '#'+id;
		            let $close = '#'+id + ' .close';
		            $($id).load(param.path + '?t=' + Math.random(), function (r) {
		                $($close).click(function () {
		                    layer.close(popupCenterIndex);
		                });
		                layui.form.render();
		                param.init ? param.init() : '';
		            });
		        },
		        btn: param.btn ? param.btn : ['操 作', '关 闭'],
		        yes: function (index, layero) {
		            param.opt ? param.opt(index) : '';
		        },
		        btn2: function (index, layero) {
			
		        },
		        end: function () {
		            param.end ? param.end() : '';
		        }
		    });
		    return popupCenterIndex;
		},
		info: function (param) {
		    sdDialog.popupCenter({
		        title: '数据详情',
		        height: (typeof param.height) == "undefined" ? '80%' : param.height,
		        width: (typeof param.width) == "undefined" ? '660px' : param.width,
		        path: '/components/common/info.html',
		        btn: [],
		        init: function () {
		            param.init ? param.init() : '';
		        }
		    });
		},
		
		finishWin: function (page, fn) {
		    layer.close(page);
		    fn ? fn() : '';
		}
	}
	exports('sdDialog', sdDialog);
})